package homework12月9号补交.homework2.d5;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {
        List<Animal> test = test();
        System.out.println(test);
    }

    public static List<Animal> test() throws Exception {
        ArrayList<Animal> animals = new ArrayList<>();
        List<String> list = FileUtils.readLines(new File("D:\\ldea dashuju\\day.12.6\\src\\czxy\\homework2\\d5\\d5.txt"));
        for (String s : list) {
            System.out.println(s);
            String[] s1 = s.split(" ");
            System.out.println(s1[0]);
            Class<?> aClass = Class.forName(s1[0]);
            Constructor<?> constructor = aClass.getConstructor();
            Object o = constructor.newInstance();
            if (o instanceof Cat){
                Cat o1 = (Cat) o;
                o1.setId(s1[1].split("=")[1]);
                o1.setName(s1[2].split("=")[1]);
                o1.setColor(s1[3].split("=")[1]);
                animals.add(o1);
            }else if (o instanceof Dog){
                Dog o1 = (Dog) o;
                o1.setId(s1[1].split("=")[1]);
                o1.setName(s1[2].split("=")[1]);
                System.out.println(Integer.parseInt(s1[3].split("=")[1]));
                o1.setAge(Integer.parseInt(s1[3].split("=")[1]));
                animals.add(o1);
            }
        }
        return animals;
    }
}
